package com.example.zegodemo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;


import java.util.Objects;


public class CaptureScreenService extends Service {

    private MediaProjection mMediaProjection;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        createNotificationChannel();

        if (intent != null) {
            int resultCode = intent.getIntExtra("code", -1);
            Intent resultData = intent.getParcelableExtra("data");
            mMediaProjection = ((MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE)).getMediaProjection(resultCode, Objects.requireNonNull(resultData));
        }
        return new Agent();
    }

    private void createNotificationChannel() {
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器

        builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)
                //.setContentTitle("SMI InstantView") // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
                .setContentText("live......") // 设置上下文内容
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

        /*以下是对Android 8.0的适配*/
        //普通notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("notification_id");
        }
        //前台服务notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channel);
        }

        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        startForeground(110, notification);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("CaptureScreenService", "onUnbind");
        stopForeground(true);
        return super.onUnbind(intent);
    }

    /**
     * 对外公开的服务方法调用接口
     */
    public interface IService {
        MediaProjection callServiceMethod();
    }

    /**
     * 服务类中私有的代理类，可以直接与服务类相互调用的类，但同时实现了IService接口，
     * 所以把该对象传递给外部时，外部看起来就是一个IService的实现类
     * 使用的是双接口的java技巧实现 对外公开部分方法
     */
    private class Agent extends Binder implements IService {
        @Override
        public MediaProjection callServiceMethod() {
            Log.e("CaptureScreenService", "callServiceMethod");
            return mMediaProjection;
        }
    }
}